
var Acan = module.exports = function(){};
Acan.mysql = function(option){
	this.option = option;
	this.mysql = mysql;
	this.client = null;
	this.link();
}
Acan.mysql.prototype.link=function(){
		if(this.mysql.createClient) {
			this.client = this.mysql.createClient(this.option);
		} else {
			this.client = new this.mysql.Client(this.option);
			this.client.connect(function(err) {
				if(err) {
					console.error('connect db ' + this.client.host + ' error: ' + err);
					process.exit();
				}
			});
		}
		return this.client;
}
Acan.mysql.prototype.test = function(){
	console.log(this.tables);
}
Acan.mysql.prototype.model = function(table){
	if(!this.tables) this.tables={};
	if(!this.tables[table]) this.tables[table]={};
	var obj = {table:''};
	var status=false;
	this.columns(table,function(data){
		obj = {
			'table':table,
			query:this.query,
			client:this.client,
			save:this.save,
			fields:data.fields,
			comment:data.comment,
			columns:data
		};
		status=true;
	});
	//while(status==false){};
	this.tables[table] = obj;
	return this.tables[table];
}
Acan.mysql.prototype.columns=function(table,callback){
	this.tables[table] = obj;
	var sql = "SHOW FULL FIELDS FROM "+table;
	var obj = {fields:[],comment:{}},key_name='';
	this.client.query(sql,function selectCb(err, rs, fields) {
		if(err){throw err;}
		for(var i in rs){
			if(rs[i].Key == 'PRI' && key_name==''){
				key_name = rs[i].Field;
			}
			obj.fields[i] = rs[i].Field;
			obj.comment[rs[i].Field] = rs[i].Comment;
		}
		obj.key_name = key_name;
		callback(obj);
	});
}
Acan.mysql.prototype.query=function(sql,callback){
	if(sql.substr(0,6)=="SELECT"){
		return this.client.query(sql,function selectCb(err, results, fields) {
			if(err){throw err;}
			if(callback){
				callback(results, fields);
			}
		});
	}else{
		return this.client.query(sql,function(err, results) {
			if(err){throw err;}
			if(callback){
				callback(results);
			}
		});
	}
}
Acan.mysql.prototype.update=function(val,con,callback,type){
	var sql = this.unisql_update(val,con,type);
	return this.client.exec(sql,callback);
}
Acan.mysql.prototype.updatePk=function(val,pk,callback){
	var sql = this.unisql_update(val,'`'+this.key_name+'`=\''+pk+'\'');
	return this.client.exec(sql,callback);
}
Acan.mysql.prototype.updateAll=function(val,con,callback,type){
	$sql = this.unisql_update(val,con,type);
	return this.client.exec($sql);
}
Acan.mysql.prototype.save=function(data,callback,type){
	if(!type) type=2;//1 To force inserted,2 To identify whether there are KEY
	var sql='',rs;
	if(typeof data == 'object'){
		if(data[this.key_name]){
			rs = this.updatePk(data,data[this.key_name]);
		}else{
			sql = this.unisql_insert(data);
		}
	}
	if(sql){
		this.client.query(sql,	function selectCb(err, results, fields) {
			if(err){throw err;}
			callback(results, fields);
		});
	}
}
//sql unite INSERT INTO
Acan.mysql.prototype.unisql_insert=function(data){
	var col='',val='';
	for(var fd in data){
		if(!data[fd] && data[fd] != 0){continue;}
		if(!Acan.inArr(fd,this.fields)){continue;}
		col += "`"+fd+"`,";
		val += "'"+data[fd]+"',";
	}
	return "INSERT INTO `"+this.table+"` ("+col.substr(0,-1)+") VALUES("+val.substr(0,-1)+")";
}
//sql unite UPDATE
Acan.mysql.prototype.unisql_update=function(val,con,type){
	if(!type) type = 'field';
	var set='',where='',select='',limit='',join='',order='',group='';
	if(typeof val=='object'){
		for(var field in val){
			if(!this.fields[field]){continue;}
			val[field] = val[field].replace(/\\\'/g,"'").replace(/\'/g,"\\'");
			set += '`'+field+'`=\''+val[field]+'\',';
		}
		set = set.substr(0,-1);
	}else if(typeof val=='string'){
		set = val;
	}
	if(typeof con=='string'){
		where = con;
	}else if(typeof con=='object'){
		for(var i in con){
			if(type=='field'){
				where += ' `'+i+'` = \''+con[i]+'\' and';
			}else{
				switch(i){
					case 'select':
						select = con[i];
					case 'limit':
						limit = con[i];
					case 'join':
						join = con[i];
					case 'group':
						group = ' GROUP BY '+con[i];
					case 'order':
						order = ' ORDER BY '+con[i];
					case 'where':
						where = con[i];
				}
			}
		}
		if(type=='field'){
			where = where.substr(0,-4);
		}else{
			where = join+where+group+by+limit;
		}
	}
	return "UPDATE `"+this.table+"` SET"+set+where;
}
